package org.thomias.exo1.tcp.client;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.net.Socket;

public class HelloClient {
	public static void main(String[] args) {
		for (int i = 0; i < 1; i++) {
			println("Essais " + i);
			request();
		}
	}

	public static void request() {
		try {

			Socket echoSocket = null;
			PrintStream out = null;
			BufferedReader in = null;
			try {
				echoSocket = new Socket();
				echoSocket.setReuseAddress(true);
				echoSocket.connect(
						new InetSocketAddress("192.168.1.97", 22223), 5000);
				println("Connection Success");

				out = new PrintStream(echoSocket.getOutputStream(), true);
				in = new BufferedReader(new InputStreamReader(System.in));
				BufferedReader serverIn = new BufferedReader(new InputStreamReader(
						echoSocket.getInputStream()));
				String input;
				while ((input = in.readLine()) != null) {
					out.println(input);
					println("Moi: " + input);
					println("Server: " + serverIn.readLine());
				}

			} finally {
				echoSocket.close();
				out.close();
				in.close();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void printError(String msg, Exception e) {
		System.err.println(msg + "\n\t" + e);
	}

	public static void println(String msg) {
		System.out.println(msg);
	}
}
